<?
function doOk() {
	global $plan_name,$cur_template,$dbh81;
	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='store_id, price, wholesale, offer_product';
		$sql['table']='products';
		$sql['where']="product_id='".$_POST['prod_id']."'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$fres=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$fpr = sprintf("%01.2f",$fres[0]['price'][12]['USD']);
	$fpr_mo = sprintf("%01.2f",$fres[0]['price'][12]['USD']/12);
	$fwhole = sprintf("%01.2f",$fres[0]['wholesale'][12]['USD']);
	$fwhole_mo = sprintf("%01.2f",$fres[0]['wholesale'][12]['USD']/12);
	$foffer = $fres[0]['offer_product'];
	$str_id = $fres[0]['store_id'];

	//------------------------------------------------------
		$sql['type']='SELECT';
		$sql['params']='products';
		$sql['table']='stores';
		$sql['where']="store_id='".$str_id."'";
		$sql['options']='';
		$sql['return']='struct';
	//------------------------------------------------------
	if (!$stres=sql_exec($sql,$dbh81,'assoc')) {
		$errorMsg = translate('int_error');
		include_once('errorMsg.html');
		exit();
	}
	$offered = array_keys($stres[0]['products']['plans_offering']);
	$okMsg = translate('successfully_edited_plan',array('plan' => "$plan_name"));
	$okMsg .= "<br>";
	/*if (in_array($_POST['prod_id'],$offered)) {
		$okMsg .= translate('plan_act',array('plan' => "$plan_name"));
		$okMsg .= "<br><ul>";
		$okMsg .= "<li>".translate('plan_can_be_purchased')."</li>";
	} else {
		$okMsg .= translate('plan_not_active',array('plan' => "$plan_name"));
		$okMsg .= "<br><ul>";
		$okMsg .= "<li>".translate('plan_cannot_be_purchased')."</li>";
	}
	if ($foffer == 't') {
		$okMsg .= "<li>".translate('plan_offered_on_template',array('template' => "$cur_template"))."</li>";
	} else {
		$okMsg .= "<li>".translate('plan_not_offered_on_template',array('template' => "$cur_template"))."</li>";
	}
	$dom_offering = array_keys($stres[0]['products']['domains_free']);
	if (in_array($_POST['prod_id'],$dom_offering)) {
		$okMsg .= "<li>".translate('plan_offered_with_domain',array('template' => "$cur_template"))."</li>";
	}
	$indexed = array_keys($stres[0]['products']['plans_on_index']);
	if (in_array($_POST['prod_id'],$indexed)) {
		$okMsg .= "<li>".translate('plan_displayed_on_index',array('template' => "$cur_template"))."</li>";
	} else {
		$okMsg .= "<li>".translate('plan_not_displayed_on_index',array('template' => "$cur_template"))."</li>";
	}*/
	$okMsg .= "<li>".translate('plan_offered_prices',array('wmprice' => $fwhole_mo,
								'wanprice' => $fwhole,
								'rmprice' => $fpr_mo,
								'ranprice' => $fpr))."</li>";
	$okMsg .= "</ul><br>";
	return $okMsg;
}

if (isset($_POST['control_type']) && $_POST['control_type'] == 3) {
	if (empty($_POST['plan_name'])) {
		$errorMsg = translate('noplanname');
		include_once('errorMsg.html');
		exit();
	} elseif (!preg_match("/^[a-zA-Z0-9\-]{1,10}$/", $_POST['plan_name'])) {
		$errorMsg = translate('planname_doesnt_match');
		include_once('errorMsg.html');
	} else {
		//------------------------------------------------------
			$sql['type']='SELECT';
			$sql['params']='currency, store_id';
			$sql['table']='stores';
			$sql['where']="reseller_id='$id'";
			$sql['options']='';
			$sql['return']='struct';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81,'assoc')) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}
		$res_currency=$result[0]['currency'];
		$store_id=$result[0]['store_id'];

		//------------------------------------------------------
			$sql['type']='SELECT';
			$sql['params']="key_name, quantity, $res_currency as currency";
			$sql['table']='plan_elements AS a, plan_prices AS b';
			$sql['where']="a.id=b.id and a.key_name!='cgi'";
			$sql['options']='order by b.id,quantity';
			$sql['return']='struct';
		//------------------------------------------------------
		if (!$result=sql_exec($sql,$dbh81,'assoc')) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		}
		$plan_name=htmlentities($_POST['plan_name']);
		$plan_name=pg_escape_string($_POST['plan_name']);
		$period=$_POST['period'];
		foreach ($_POST as $key => $value) {
			if ($key!='plan_name' && $key!='period' && $key!='submit' && $key!='form_sent') {
				if (preg_match("/^key_.*/",$key)) {
					$key=preg_replace("/^(key_)/",'',$key);
					$newarr[$key]=$value;
				}
			}
		}
		foreach ($newarr as $key => $value) {
			if ($key=='disk_space') $value=$value*1024;
			if ($key=='traffic') $value=$value*1024000;
			$services[$key]=$value;
		}
		$tmp=$services['mailbox']*2;
		$services['aliases']=$tmp;
		$services['fpe']=9999;
		$services['ppa']=9999;
		#$services['site_studio']=0;
		$services['ssl']=9999;
		$services['filemanager']=1;
		$services['stats']=1;
		$services['redirect_url']=10;
		$services['backup']='-1';
		$services['webmail']=1;
		$services['phpmy_admin']=1;
		#$services['support']=1;
		$services['template']='-1';
		$services['smtp']=1;
		$services['register_domain']=1;
		#$tmp=$services[domain]*2;
		#$services[subdomain]=$tmp;
		$tmp=$services['sql']*30;
		$services['sql_quota']=$tmp;
		foreach ($result as $rslt) {
			if ($newarr[$rslt['key_name']]==$rslt['quantity'])
				$prices[$rslt['key_name']]=$rslt['currency'];
		}
		$monpr=sprintf("%01.2f",array_sum($prices));
		$minpr=sprintf("%01.2f",$monpr + ($monpr*5)/100);
		$maxpr=sprintf("%01.2f",$monpr + ($monpr*125/100));

		if ($_POST['new_price'] == '') {
			$errorMsg = translate('no_price_set');
			include_once('errorMsg.html');
			$hide_rest = 'yes';
		} elseif ($_POST['new_price'] > $maxpr) {
			$errorMsg = translate('above_maximal_price_2');
			include_once('errorMsg.html');
			$hide_rest = 'yes';
		} elseif ($_POST['new_price'] < $minpr) {
			$errorMsg = translate('below_minimal_price_2');
			include_once('errorMsg.html');
			$hide_rest = 'yes';
		} else {
			$min_price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$max_price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$wholesale=array(1=>'',3=>'',6=>'',9=>'',12=>'');
			$min_price[$period][$res_currency]=sprintf("%01.2f",$minpr*$period);
			$max_price[$period][$res_currency]=sprintf("%01.2f",$maxpr*$period);
			$wholesale[$period][$res_currency]=sprintf("%01.2f",$monpr*$period);
			$price[$period][$res_currency]=sprintf("%01.2f",$_POST['new_price']*$period);
			$min_price=base64_encode(serialize($min_price));
			$max_price=base64_encode(serialize($max_price));
			$price=base64_encode(serialize($price));
			$wholesale=base64_encode(serialize($wholesale));
			$services=base64_encode(serialize($services));

			$sql="UPDATE products SET name='$plan_name',
				services='$services',
				min_price='$min_price',
				max_price='$max_price',
				price='$price',
				wholesale='$wholesale'
			WHERE
				product_id='".$_POST['prod_id']."'
				AND reseller_id='$id'";
			if (!$rsut=pg_query($dbh81,$sql)) {
				$errorMsg = translate('int_error');
				include_once('errorMsg.html');
				exit();
			} else {
				$okMsg = doOk();
				include_once('okMsg.html');
			}
			unset($sql);
		}
	}
} else {
	//------------------------------------------------------
                $sql['type']='SELECT';
                $sql['params']='name, min_price, max_price';
                $sql['table']='products';
                $sql['where']="product_id='$prod_id'";
                $sql['options']='';
                $sql['return']='struct';
        //------------------------------------------------------
        if (!$result=sql_exec($sql,$dbh81,'assoc')) {
                $errorMsg = translate('int_error');
                include_once('errorMsg.html');
                exit();
        }
	$plan_name = $result[0]['name'];
	$minpr = sprintf("%01.2f",$result[0]['min_price'][12]['USD']/12);
	$maxpr = sprintf("%01.2f",$result[0]['max_price'][12]['USD']/12);

	if ($_POST['new_price'] == '') {
		$errorMsg = translate('no_price_set');
		include_once('errorMsg.html');
		$hide_rest = 'yes';
	} elseif ($_POST['new_price'] > $maxpr) {
		$errorMsg = translate('above_maximal_price_2');
		include_once('errorMsg.html');
		$hide_rest = 'yes';
	} elseif ($_POST['new_price'] < $minpr) {
		$errorMsg = translate('below_minimal_price_2');
		include_once('errorMsg.html');
		$hide_rest = 'yes';
	} else {
		$price=array(1=>'',3=>'',6=>'',9=>'',12=>'');
		$price[12]['USD']=sprintf("%01.2f",$_POST['new_price']*12);
		$price=base64_encode(serialize($price));
		$sql="UPDATE products SET price='$price'
			WHERE
				product_id='".$_POST['prod_id']."'
				AND reseller_id='$id'";
		if (!$rsut=pg_query($dbh81,$sql)) {
			$errorMsg = translate('int_error');
			include_once('errorMsg.html');
			exit();
		} else {
			$okMsg = doOk();
			include_once('okMsg.html');
		}
	}
}
?>
